﻿<?php
   require_once('contest.class.php'); 
   require_once( 'contest_problem_list.class.php' );  
   require_once( 'contest_team.class.php' );         
   /**
   *
   *创建新的竞赛 
   *添加试题   
   *@author kinet 380511703@qq.cn
   *@time 2010-5-10
   *@version 1.1
   */      
   class contest_create{
         protected $contest_no;
         protected $contest;
         private $contest_problem;
         protected $db;
         /**
         * 创建一个新的竞赛
         * 
         * @param 数据库
         * @param 竞赛名
         * @param 开始时间
         * @param 结束时间
         * @param概要
         * @param 提交错误每到题加时
         */
         public function contest_create($db,$vname,$vsign_begin_time,$vsign_end_time,$vbegin_time,$vend_time,$vsummary,$vaddtime,$vstatus){
               $this->contest=new contest($db);
               $this->db=$db; 
               $this->contest->set('name',$vname);
 	       $this->contest->set('sign_begin_time',$vsign_begin_time);
               $this->contest->set('sign_end_time',$vsign_end_time);
               $this->contest->set('begin_time',$vbegin_time);
               $this->contest->set('end_time',$vend_time);
               $this->contest->set('summary',$vsummary);
               $this->contest->set('addtime',$vaddtime);  
  	       $this->contest->set('status',$vstatus);    
               
               if($this->contest->init_contest_no()==-1){                 
                //如果竞赛名不存在继续
                $this->contest->save();                                     
               //保存入库
               $this->contest_no=$this->contest->init_contest_no();
               //获得刚创建竞赛的编号                                    
              }
              else{
                                     
                             //竞赛名已存在
                             throw new Exception("竞赛名已存在",1);  
              }
         }
      /**
               * 添加试题
               *  添加成功返回true 失败返回 false
               * @param 题号
               */
        public function  add_problem($problem_no){ 
                try{
                      $this->is_the_probelm_can_add_in($problem_no);// 判断是否能够添加
                }
                catch(Exception $e){ 
                       throw new Exception($e->__toString(),4);     //不能添加抛出异常
                }              
                $this->contest_problem=new contest_problem_list($this->db);
                $this->contest_problem->set('contest_no',$this->contest_no);
                $this->contest_problem->set('problem_no',$problem_no);
                $this->contest_problem->save();                  
              
       }
           /**
               * 检测是否题号存在
               * 或者已添加了              
               * @param mixed $problem_no
               */
        protected function is_the_probelm_can_add_in($problem_no){   
                $sql="select * 
                      from problem  
                      where  
                      problem_no=".$problem_no;
                $res = $this->db->query( $sql ) ;  
                if(PEAR::isError($res)) {                       
                    throw new Exception("题库中没有该题号",2);                      
                }
                $rows = $res->fetchRow( MDB2_FETCHMODE_ASSOC );
                if ( !$rows ){
                         throw new Exception("题库中没有该题号",2);
                         return false;                      
                }
                 
                $iscontest_problem=new contest_problem_list($this->db);
                if( $iscontest_problem->load($this->contest_no,$problem_no)==false)
                   return true; 
                 else{ 
                   throw new Exception("竞赛中中有该题号",2);
                    return false; 
                 }                  
          }               
        /**
        * 删除试题 
        * @param mixed $problem_no
        */
        public function del_problem($problem_no){
              $iscontest_problem=new contest_problem_list($this->db);
              if( $iscontest_problem->load($this->contest_no,$problem_no)==false){
                        throw new Exception("已删除",4);
                        return false;
              }
               else{ 
                   $iscontest_problem->del($this->contest_no,$problem_no); 
               }
              return true;
        }
        public function del_contest(){      
               if($this->contest->init_contest_no()==-1){                 
                    throw new Exception("已删除",4); 
                     return false;      
               }
               $this->contest->delete('name', $this->contest->get('name'));
               return true;
        }
        /**
        * 修改本次竞赛信息
        * 
        * @param 竞赛名
        * @param 开始时间
        * @param 结束时间
        * @param 概要
        */
        public function update_contest($db,$vname,$vsign_begin_time,$vsign_end_time,$vbegin_time,$vend_time,$vsummary,$vaddtime,$vstatus){   
                
                $sql="update contest 
                      set name='$vname',
                      sign_begin_time=$vsign_begin_time,
                      sign_end_time=$vsign_end_time,
 		      begin_time=$vbegin_time,
                      end_time=$vend_time,
                      summary=$vsummary,
                      addtime=$vaddtime
                      where contest_no='".$this->contest_no."'";                  
                 $res = $this->db->query( $sql ) ; 
                    if(PEAR::isError($res)) {  
                        return false ;
                      }   
                  return true;                   
        }
        public function add_team($team_no){
               try {
                      $this->is_the_team_can_add_in($team_no); //判断是否可添加
                }
                catch(Exception $e){
                       throw new Exception($e->__toString(),4); 
                }              
                $contest_team=new  contest_team($this->db);
                $contest_team->set('contest_no',$this->contest_no);
                $contest_team->set('team_no',$team_no);
                $contest_team->save();                  
              
              
        } 
         protected function is_the_team_can_add_in($team_no){   
                $sql="select * 
                      from team  
                      where  
                      team_no=".$team_no;
                $res = $this->db->query( $sql ) ;  
                if(PEAR::isError($res)) {
                    throw new Exception("队伍表中没有该队号",1);                      
                }
                $rows = $res->fetchRow( MDB2_FETCHMODE_ASSOC );
                if ( !$rows ){   
                         throw new Exception("队伍表中没有该队号",2);
                         return false;                      
                } 
                $iscontestteam=new contest_team($this->db);
                if($iscontestteam->load($this->contest_no,$team_no)==false)//没有
                   return true;  
                 else{
                      throw new Exception("队伍已经在竞赛",3);
                       return false;     
                 }                     
              }
          public function del_team($team_no){
              $iscontestteam=new contest_team($this->db);     
                if($iscontestteam->load($this->contest_no,$team_no)==false) {                   
                      throw new Exception("已删除",4);
                      return false;
            }
            $iscontestteam->del();
            return true;
            
        }
            
            
                   
            
             
   }
   
?>